/*
 * This program is non-free software, but you can redistribute it and/or modify
 * it if you are in hjj(Huangshi Hejianjiao) team.
 */
package com.truthbean.jutils.net;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.sd4324530.fastweixin.api.response.BaseResponse;
import com.github.sd4324530.fastweixin.util.JSONUtil;
import com.github.sd4324530.fastweixin.util.NetWorkCenter;
import java.util.Map;
import java.util.Objects;
import org.apache.log4j.Logger;

/**
 *
 * @author TruthBean
 */
public class NetWorkUtils {

    private static final Logger logger = Logger.getLogger(NetWorkUtils.class.getName());

    public static JSONObject netGet(String url) {
        logger.info("netGet url: " + url);
        try {
            BaseResponse response = NetWorkCenter.get(url);
            if (Objects.equals(response.getErrcode(), "0")) {
                JSONObject object = JSON.parseObject(response.getErrmsg());
                logger.info("response result: " + object.toJSONString());
                if (object.containsKey("code")) {
                    Object code = object.get("code");
                    if (code instanceof String && code.equals("0")) {
                        return object;
                    }
                }
            }
        } catch (Exception ex) {
            logger.error(ex);
            return null;
        }

        return null;
    }

    public static JSONObject netPost(String url, String paramData) {
        logger.info("netPost url: " + url);
        try {
            BaseResponse response = NetWorkCenter.post(url, paramData);
            if (response.getErrcode() == null || "0".equals(response.getErrcode())) {
                JSONObject object = JSON.parseObject(response.getErrmsg());
                logger.info("response result: " + object.toJSONString());
                if (object.containsKey("code")) {
                    Object code = object.get("code");
                    if (code instanceof String && code.equals("0")) {
                        return object;
                    }
                }
            }
        } catch (Exception ex) {
            logger.error(ex);
            return null;
        }
        return null;
    }

    public static JSONObject netPost2(String url, String paramData) {
        logger.info("netPost url: " + url);
        try {
            BaseResponse response = NetWorkCenter.post(url, paramData);
            if (response.getErrcode() == null || "0".equals(response.getErrcode())) {
                JSONObject object = JSON.parseObject(response.getErrmsg());
                logger.info("response result: " + object.toJSONString());
                return object;
            }
        } catch (Exception ex) {
            logger.error(ex);
            return null;
        }
        return null;
    }

    public static Map<String, Object> doGet(String url) {
        try {
            BaseResponse response = NetWorkCenter.get(url);
            return JSONUtil.toMap(response.getErrmsg());
        } catch (Exception ex) {
            logger.error(ex);
            return null;
        }

    }

    public static Map<String, Object> doPost(String url, Map<String, Object> params) {
        try {
            String json = JSONUtil.toJson(params);
            BaseResponse response = NetWorkCenter.post(url, json);
            return JSONUtil.toMap(response.getErrmsg());
        } catch (Exception ex) {
            logger.error(ex);
            return null;
        }

    }

}
